package urine;

import java.awt.*;

import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.*;

import java.awt.event.*;

import javax.imageio.*;
import java.io.*;


public class UrineControl extends JFrame
implements ActionListener
{
	private static final long serialVersionUID = 1L;
	private JPanel panel;
	private Image crossHairImage, backgroundImage, brownDogImage,
	offscreenImage,blackDogImage,ponyImage,brownDogDeadImage,
	blackDogDeadImage;
	private JButton newGameButton;
	private int countDown, mousePressedX,mousePositionX,mousePositionY,
	panelX,panelY,currentlyPlaying;
	private JLabel scoreLabel, timeLabel, scoreValLabel,timeValLabel;
	private Timer gameTimer,badDogTimer;
	private WavPlay audio1, audio2, audio3, audio4, audio5;
	private JMenuBar menuBar;
	private JMenu menu;
	private JMenuItem menuItem1, menuItem2, menuItem3, menuItem4, menuItem5;
	private BadDog badDog;
	private int points;
	private Dimension d;


	public static void main(String[] args) throws UnsupportedAudioFileException, LineUnavailableException, IOException 
	{
		try
		{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch(Exception e)
		{
			throw new RuntimeException(e);
		}
		UrineControl frame = new UrineControl();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	/**
	 * Sole Constructor
	 */
	public UrineControl() throws UnsupportedAudioFileException, LineUnavailableException, IOException
	{
		super("UrineControl - Thomas Wolfe 2008");
		panelX=640;
		panelY=480;
		mousePressedX=1;
		mousePositionX=1;
		setSize(800,600);
		setVisible(true);
		Container window = getContentPane();
		window.setLayout(new FlowLayout());
		panel = new JPanel();
		d=new Dimension(panelX,panelY);
		panel.setPreferredSize(d);
		window.setBackground(Color.pink);
		window.add(panel);
		newGameButton = new JButton("Start Game");
		newGameButton.addActionListener(
				new ActionListener()
				{
					// event handler called when newGameButton is pressed
					public void actionPerformed( ActionEvent event )
					{
						// reset counters
						points = 0;
						countDown = 30;

						// reset labels
						scoreValLabel.setText( String.valueOf( points ) );
						timeValLabel.setForeground( Color.BLACK );
						timeValLabel.setText( String.valueOf( countDown ) );

						newGameButton.setEnabled( false ); // disable newGameJButton

						// start the moleTimer and gameTimer
						badDogTimer.start();
						gameTimer.start();

						repaint();
					}

				} // end anonymous inner class

		); // end call to addActionListener
		window.add(newGameButton);

		try
		{
			File crossHairFile = new File("/home/thomas/Desktop/crosshair.png");
			crossHairImage = ImageIO.read(crossHairFile);

			File backgroundFile = new File("/home/thomas/Desktop/field.png");
			backgroundImage = ImageIO.read(backgroundFile).getScaledInstance(panelX, panelY, 1);

			File blackDogFile = new File("/home/thomas/Desktop/blackdog.png");
			blackDogImage = ImageIO.read(blackDogFile).getScaledInstance(75,75,1);

			File brownDogFile = new File("/home/thomas/Desktop/browndog.png");
			brownDogImage = ImageIO.read(brownDogFile);
			
			File blackDogDeadFile = new File("/home/thomas/Desktop/blackdogdead.png");
			blackDogDeadImage = ImageIO.read(blackDogDeadFile).getScaledInstance(75,75,1);

			File brownDogDeadFile = new File("/home/thomas/Desktop/browndogdead.png");
			brownDogDeadImage = ImageIO.read(brownDogDeadFile);

			File ponyFile = new File("/home/thomas/Desktop/pony.png");
			ponyImage = ImageIO.read(ponyFile).getScaledInstance(75,75,1);
		}
		catch (Exception e)
		{
			//e.printStackTrace();
			System.out.println("Unable to locate file(s)");
		}

		Toolkit t = Toolkit.getDefaultToolkit();
		Cursor aim = t.createCustomCursor (crossHairImage, new Point(1,1),"aim");
		setCursor(aim);

		// Menu
		menuBar = new JMenuBar();
		window.add(menuBar);
		menu = new JMenu("Choose Song");
		menuBar.add(menu);
		menuItem1 = new JMenuItem("Silversun Pickups - Lazy Eye");
		menuItem1.addActionListener(this);
		menu.add(menuItem1);
		menuItem2 = new JMenuItem("Temple Of The Dog - Say Hello To Heaven");
		menuItem2.addActionListener(this);
		menu.add(menuItem2);
		menuItem3 = new JMenuItem("Pixies - Where is My Mind?");
		menuItem3.addActionListener(this);
		menu.add(menuItem3);
		menuItem4 = new JMenuItem("Monzy - So Much Drama in the PhD");
		menuItem4.addActionListener(this);
		menu.add(menuItem4);
		menuItem5 = new JMenuItem("Stop Song");
		menuItem5.addActionListener(this);
		menu.add(menuItem5);

		scoreLabel = new JLabel("Score: ");
		window.add(scoreLabel);
		scoreValLabel = new JLabel("");
		window.add(scoreValLabel);
		timeLabel = new JLabel("Time Left: ");
		window.add(timeLabel);
		timeValLabel = new JLabel("");
		window.add(timeValLabel);
		badDog = new BadDog(panelX,panelY);

		// set up badDogTimer
		badDogTimer = new Timer(1200, new ActionListener()
		{
			// timer has incremented
			public void actionPerformed( ActionEvent event )
			{
				badDog.moveDog();

				repaint();
			}
		} // end anonymous inner class

		); // end new Timer

		// set up gameTimer
		gameTimer = new Timer( 500, new ActionListener()
		{
			// timer has incremented
			public void actionPerformed( ActionEvent event )
			{
				countDown--; // decrement counter

				// display time remaining
				timeValLabel.setText( String.valueOf( countDown ) );

				// if less than ten seconds are remaining
				if ( countDown < 10 )
				{
					// change the color of the text in timeJTextField to red
					timeValLabel.setForeground( Color.RED );
				}

				// when time runs out
				if ( countDown == 0 )
				{
					// stop timers
					gameTimer.stop();
					badDogTimer.stop();

					newGameButton.setEnabled( true ); // enable newGameJButton

					// display score
					JOptionPane.showMessageDialog( null, "Your score is " +
							points + " points", "Game Over", 
							JOptionPane.INFORMATION_MESSAGE );

				} // end if
			}
		} // end anonymous inner class

		); // end new Timer

		addMouseListener( 

				new MouseAdapter() // anonymous inner class
				{
					// event handler called when mouse button is pressed
					public void mousePressed( MouseEvent event )
					{
						mousePressedX=event.getX();
						repaint();
					}

				} // end anonymous inner class

		); // end call to addMouseListener
		addMouseMotionListener( 

				new MouseAdapter() // anonymous inner class
				{
					// event handler called when mouse button is pressed
					public void mouseMoved( MouseEvent event )
					{
						mousePositionX=event.getX();
						mousePositionY=event.getY();
						repaint();
					}

				} // end anonymous inner class

		); // end call to addMouseListener
	}


	public void actionPerformed(ActionEvent event)
	{
		Object B = event.getSource();
		if (B == menuItem1)
		{
			try {
				audio5.close();
				audio1 = new WavPlay("/home/thomas/Desktop/lazyEye.wav");
				audio1.playonce();
				currentlyPlaying=1;
			} catch (LineUnavailableException e) {
				// TODO Auto.loop(Clip.LOOP_CONTINUOUSLY);-generated catch block
				e.printStackTrace();
			} catch (UnsupportedAudioFileException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (B == menuItem2)
		{
			try {
				audio5.close();
				audio2 = new WavPlay("/home/thomas/Desktop/sayhellotoheaven.wav");
				audio2.play();
				currentlyPlaying=2;
			} catch (LineUnavailableException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedAudioFileException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		if (B == menuItem3)
		{
			try {
				audio5.close();
				audio3 = new WavPlay("/home/thomas/Desktop/pixies.wav");
				audio3.play();
				currentlyPlaying=3;
			} catch (LineUnavailableException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedAudioFileException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		if (B == menuItem4)
		{
			try {
				audio5.close();
				audio4 = new WavPlay("/home/thomas/Desktop/drama.wav");
				audio4.play();
				currentlyPlaying=4;
			} catch (LineUnavailableException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedAudioFileException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		if (B == menuItem5)
		{
			switch(currentlyPlaying)
			{
			case 0:
				System.out.println("nothing is playing(switch)");
			case 1:
				audio1.close();
				break;
			case 2:
				audio2.close();
				break;
			case 3:
				audio3.close();
				break;
			case 4:
				audio4.close();
				break;
			case 5:
				audio5.close();
				break;
			}
		}
	}
	
	/**
	 * makes sure there is an offscreen image for
	 * double buffering and if there is not
	 * it will create one
	 */
	private void checkOffscreenImage()
	{
		if (offscreenImage == null || offscreenImage.getWidth(null) != d.width
				|| offscreenImage.getHeight(null) != d.height)
		{
			offscreenImage = createImage(d.width, d.height);
		}
	}
	
	/**
	 * does all of the drawing off screen
	 * before actually painting thus eliminating
	 * flickering
	 */
	public void paintOffscreen(Graphics g)
	{
		g.drawImage(backgroundImage, 35, 28, this);
		
		g.drawImage(ponyImage, mousePositionX,25,this);

		// if dog is hit
		if ( badDog.isHit( mousePressedX ) )
		{
			if(badDog.whichDog()==0)
			{
				g.drawImage(brownDogDeadImage, badDog.getDogX(),
						badDog.getDogY(), this);
			}
			else
			{
				g.drawImage(blackDogDeadImage, badDog.getDogX(),
						badDog.getDogY(), this);
			}
			if(Math.abs(badDog.getDogX()+15-mousePressedX)<=10)
			{
				points+=100;
				g.setColor(Color.ORANGE);
				g.drawString("Good Shot", mousePositionX, mousePositionY);
			}
			else if(Math.abs(badDog.getDogX()+15-mousePressedX)<=20)
			{
				points+=200;
				g.setColor(Color.ORANGE);
				g.drawString("Great Shot", mousePositionX, mousePositionY);
			}
			else
			{
				points+=50;
			}

			// display new score
			scoreValLabel.setText( String.valueOf( points ) );

			badDogTimer.restart(); // restart the badDogTimer
			g.setColor(Color.ORANGE);
			g.drawLine(mousePressedX+25, 90, badDog.getDogX()+15, badDog.getDogY());
			try {
				audio5 = new WavPlay("/home/thomas/Desktop/bark.wav");
				audio5.playonce();
				currentlyPlaying=5;
			} catch (LineUnavailableException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnsupportedAudioFileException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			// display next dog
			mousePressedX=5000;
			badDog.moveDog();
		}
		if(badDog.whichDog()==0)
		{
			g.drawImage(brownDogImage, badDog.getDogX(),
					badDog.getDogY(), this);
		}
		else
		{
			g.drawImage(blackDogImage, badDog.getDogX(),
					badDog.getDogY(), this);
		}
	}
	
	/**
	 * this is the key to double
	 * buffering. before painting
	 * it calls update()
	 */
	public void update(Graphics g)
	{
		paint(g);
	}
	/**
	 * where all the double buffering stuff
	 * comes together.
	 */
	public void paint(Graphics g)
	{
		// Clear the offscreen image.
		checkOffscreenImage();
		Graphics offG = offscreenImage.getGraphics();
		offG.setColor(getBackground());
		offG.fillRect(0, 0, d.width, d.height);
		// Draw into the offscreen image.
		paintOffscreen(offscreenImage.getGraphics());
		// Put the offscreen image on the screen.
		g.drawImage(offscreenImage, 0, 0, null);
	}
}
